#include "RemoveBigFiles.h"


RemoveBigFiles::RemoveBigFiles(void)
{

	cout << "Begin remove big files !\n";
	//cin >> repertory;
	
	//windows types because dirent.h doesn't exist in Visual Studio...
	   WIN32_FIND_DATA FindFileData;
	   HANDLE hFind;

	  
	  // _tprintf (TEXT("Target file is %s\n"),repertory);
	   hFind = FindFirstFile("*", &FindFileData);


	   if (hFind == INVALID_HANDLE_VALUE) 
	   {
		  printf ("FindFirstFile failed (%d)\n", GetLastError());
		  return;
	   } 
	   else 
	   {
		   
		   
		   //skip useless repertories
		   string nameRep = FindFileData.cFileName;
		   while(nameRep == "." || nameRep == ".."|| nameRep == ".svn"){
				FindNextFile(hFind, &FindFileData);
				if (hFind == INVALID_HANDLE_VALUE) 
				{
					printf ("FindFirstFile failed (%d)\n", GetLastError());
					return;
				} 
				nameRep = FindFileData.cFileName;
				
		   }

		   do{

			   //we get the first user file with datas
			  _tprintf (TEXT("The file found is %s\n"), FindFileData.cFileName);
		  

			  //now go inside and enter the trajectory file..
			   WIN32_FIND_DATA FindFileDataInUser;
			   HANDLE hFindInUser;

			   //create the path for the repertory
			   char* path = FindFileData.cFileName;
		   
			   strcat(path, "\\" );
			   strcat(path, "Trajectory");


			   _tprintf (TEXT("Target file is %s\n"), path);
			   hFindInUser = FindFirstFile(path, &FindFileDataInUser);
			   if (hFindInUser == INVALID_HANDLE_VALUE) 
			   {
				  printf ("FindFirstFile failed (%d)\n", GetLastError());
				  return;
			   } 
			   else 
			   {
				   char basePath[255];
				   strcpy(basePath, path);

				   strcat(path, "\\" );
				   strcat(path, "*" );


			   
				   WIN32_FIND_DATA FindFileDataInTraj;
				   HANDLE hFindInTraj;

				   _tprintf (TEXT("Target file is %s\n"), path);

				   //take first file
				   hFindInTraj = FindFirstFile(path, &FindFileDataInTraj);


				   if (hFindInTraj == INVALID_HANDLE_VALUE) 
				   {
					  printf ("FindFirstFile failed (%d)\n", GetLastError());
					  return;
				   } 
				   else 
				   {

					   //skip useless repertories
					   nameRep = FindFileDataInTraj.cFileName;
					   while(nameRep == "." || nameRep == ".."|| nameRep == ".svn"){
							if(FindNextFile(hFindInTraj, &FindFileDataInTraj)){
								if (hFind == INVALID_HANDLE_VALUE) 
								{
									printf ("FindFirstFile failed (%d)\n", GetLastError());
									return;
								} 
								nameRep = FindFileDataInTraj.cFileName;
							}
					   }

				   
					   char pathFile[255];
				   
				   


					   
					   //same with all the other ones
					   do{

							//path for the current file
							strcpy(pathFile, basePath);
							//create the path
							strcat(pathFile, "\\");
							strcat(pathFile, FindFileDataInTraj.cFileName);

							//have to be const for the constructor of trajectory
							const char * fileName = pathFile;
						
							DWORD highPart = FindFileDataInTraj.nFileSizeHigh;
							DWORD lowPart = FindFileDataInTraj.nFileSizeLow;

							int size =  (highPart * (MAXDWORD+1)) + lowPart;

							//cout << "name of the file : " << FindFileDataInTraj.cFileName <<  endl;
						//	cout << "file size : " << size << endl;
							

							if(size > 15000){
								
								//delete the file
								const char* fileToDelete = fileName;
								remove(fileToDelete);
								
							}

							
							
					   }while(FindNextFile(hFindInTraj, &FindFileDataInTraj));
				   
					  

					   FindClose(hFindInTraj);
				   }
			   

				   FindClose(hFindInUser);
			   }
		   
		   }while(FindNextFile(hFind, &FindFileData));

		  FindClose(hFind);
	   }

	   cout << "End\n";



}


RemoveBigFiles::~RemoveBigFiles(void)
{
}
